This tutorials shows how to use Canvas and drawPoints() to draw multiple Lines by setting PointMode.Lines.
For each line we define starting and ending point in a List<Offset>.
Syntax
import androidx.compose.foundation.Canvas
Canvas(Modifier.fillMaxSize()) {
drawPoints(
points = listOf(
Offset(x=500f, y=400f), Offset(x=700f, y=400f),
Offset(x=500f, y=600f), Offset(x=700f, y=600f)
),
pointMode = PointMode.Lines,
brush = SolidColor(Color.Red),
strokeWidth = 10f
)
}
In this example we use Canvas to draw multiple Lines.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PointMode
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Canvas(Modifier.fillMaxSize()) {
drawPoints(
points = listOf(
Offset(x=500f, y=400f), Offset(x=700f, y=400f),
Offset(x=500f, y=600f), Offset(x=700f, y=600f)
),
pointMode = PointMode.Lines,
brush = SolidColor(Color.Red),
strokeWidth = 10f
)
}
}
}
}